package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.modular.system.entity.AppOrderInfo;
import cn.stylefeng.guns.modular.system.enums.ResultEnum;
import cn.stylefeng.guns.modular.system.service.AppOrderInfoService;
import cn.stylefeng.guns.modular.system.service.ProxyUserService;
import cn.stylefeng.guns.modular.system.utils.ResultVOUtil;
import cn.stylefeng.guns.modular.system.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @ClassName OrderInfoController
 * @Author lzy
 * @Date 2020/1/12 3:40
 * @VERSION 1.0
 **/
@Controller
@RequestMapping("/orderInfo")
public class AppOrderInfoController {
    private final static Logger log = LoggerFactory.getLogger(AppOrderInfoController.class);

    @Autowired
    private AppOrderInfoService appOrderInfoService;
    @Autowired
    private ProxyUserService proxyUserService;
    private String PREFIX = "/modular/system/orderInfo/";

    /**
     * 跳转到主页面
     *
     * @author wg
     * @Date 2019-06-20
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "orderInfo.html";
    }

    /**
     * 订单列表
     * @param infoUserId    orderinfo的用户id
     * @param orderNo   订单编号
     * @param startTime 创建时间区间
     * @param endTime
     * @param status    订单状态
     * @return
     */
    @RequestMapping("/selectMyOrderInfo")
    @ResponseBody
    public ResultVO selectMyOrderInfo(
                                      @RequestParam("page")Integer pageNum, @RequestParam("limit") Integer pageSize,
                                      @RequestParam(value = "infoUserId", required = false) Long infoUserId,
                                      @RequestParam(value = "orderNo", required = false) String orderNo,
                                      @RequestParam(value = "startTime", required = false) String startTime,
                                      @RequestParam(value = "endTime", required = false) String endTime,
                                      @RequestParam(value = "status", required = false) String status){
        Long userId = LoginContextHolder.getContext().getUserId();
        Assert.isTrue(!ObjectUtils.isEmpty(userId), ResultEnum.PROXYID_DOES_NOT_EXIST.getMessage());
        PageHelper.startPage(pageNum,pageSize);
        List<String> subUserIds = proxyUserService.findSubUserIdList(userId.toString());
        //查询用户代理中加入自己的id
        subUserIds.add(userId.toString());
        List<Map<Object,Object>> syss = appOrderInfoService.findListByCondition(startTime,endTime,orderNo,infoUserId,status,subUserIds);
        PageInfo<Map<Object,Object>> pageInfo = new PageInfo<>(syss);
        List<Map<Object, Object>> list = pageInfo.getList();
        long total = pageInfo.getTotal();
        return ResultVOUtil.success(list,total);
    }

}
